package com.huluobo.day11_im.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.huluobo.day11_im.IMApp;
import com.huluobo.day11_im.R;
import com.huluobo.day11_im.activity.ChatActivity;
import com.huluobo.day11_im.bean.ContactListItemBean;
import com.huluobo.day11_im.chat.view.MyChatActivity;

import java.util.List;

public class MyContactListAdapter extends RecyclerView.Adapter<MyContactListAdapter.MyContactListViewHolder> {
    private List<ContactListItemBean> listItemBeans;
    private Context context;

    public MyContactListAdapter(List<ContactListItemBean> listItemBeans, Context context) {
        this.listItemBeans = listItemBeans;
        this.context = context;

    }

    @NonNull
    @Override
    public MyContactListAdapter.MyContactListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyContactListViewHolder(LayoutInflater.from(context).inflate(R.layout.item_contact_list, parent,
                false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyContactListAdapter.MyContactListViewHolder holder,
                                 int position) {
        String username = listItemBeans.get(position).getUsername();
        holder.username.setText(username);
        if (listItemBeans.get(position).isShowFirstLetter()) {
            holder.firstLetter.setVisibility(View.VISIBLE);
            holder.firstLetter.setText(listItemBeans.get(position).getFirstLetter() + "");
        } else {
            holder.firstLetter.setVisibility(View.GONE);
        }
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MyChatActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(IMApp.CHAT_CONVERSATION_ID, username);
                bundle.putInt(IMApp.CHAT_TYPE, IMApp.CHAT_TYPE_SINGLE);
                intent.putExtras(bundle);
//                intent.putExtra(IMApp.CHAT_CONVERSATION_ID, username);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (listItemBeans.size() == 0) {
            return 0;
        }
        return listItemBeans.size();
    }

    class MyContactListViewHolder extends RecyclerView.ViewHolder {
        TextView username, firstLetter;
        LinearLayout linearLayout;

        public MyContactListViewHolder(@NonNull View itemView) {
            super(itemView);
            username = itemView.findViewById(R.id.item_username);
            firstLetter = itemView.findViewById(R.id.item_firstLetter);
            linearLayout = itemView.findViewById(R.id.item_ll);
        }
    }
}
